using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The StudentAttendanceDetailDB class is responsible for interacting with the database to retrieve and store information
    /// about StudentAttendanceDetail objects.
    /// </summary>
    public class StudentAttendanceDetailDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of StudentAttendanceDetail from the underlying datasource.
        /// </summary>
        /// <param name="attKey">The unique AttKey of the StudentAttendanceDetail in the database.</param>
        /// <returns>An StudentAttendanceDetail when the AttKey was found in the database, or null otherwise.</returns>
        public static StudentAttendanceDetail GetItem(long attKey)
        {
            StudentAttendanceDetail myStudentAttendanceDetail = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAttendanceDetailSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@attKey", attKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentAttendanceDetail = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentAttendanceDetail;
            }
        }


        /// <summary>
        /// Gets an instance of StudentAttendanceDetail from the underlying datasource.
        /// </summary>
        /// <param name="attKey">The unique AttKey of the StudentAttendanceDetail in the database.</param>
        /// <returns>An StudentAttendanceDetail when the AttKey was found in the database, or null otherwise.</returns>
        public static StudentAttendanceDetail GetItemByStudentProfileKey(long attKey, int StudentProfileKey)
        {
            StudentAttendanceDetail myStudentAttendanceDetail = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAttendanceDetailSelectSingleItemByStudentProfileKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@attKey", attKey);
                    myCommand.Parameters.AddWithValue("@studentProfileKey", StudentProfileKey);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentAttendanceDetail = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentAttendanceDetail;
            }
        }

        /// <summary>
        /// Returns a list with StudentAttendanceDetail objects.
        /// </summary>
        /// <returns>A generics List with the StudentAttendanceDetail objects.</returns>
        public static StudentAttendanceDetailList GetList()
        {
            StudentAttendanceDetailList tempList = new StudentAttendanceDetailList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAttendanceDetailSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a StudentAttendanceDetail in the database.
        /// </summary>
        /// <param name="myStudentAttendanceDetail">The StudentAttendanceDetail instance to save.</param>
        /// <returns>The new AttKey if the StudentAttendanceDetail is new in the database or the existing AttKey when an item was updated.</returns>
        public static int Save(StudentAttendanceDetail myStudentAttendanceDetail)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAttendanceDetailInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myStudentAttendanceDetail.AttDetailKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@attDetailKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@attDetailKey", myStudentAttendanceDetail.AttDetailKey);
                    }

                    if (myStudentAttendanceDetail.AttKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@attKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@attKey", myStudentAttendanceDetail.AttKey);
                    }
                    if (myStudentAttendanceDetail.StudentProfileKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@studentProfileKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@studentProfileKey", myStudentAttendanceDetail.StudentProfileKey);
                    }
                    if (myStudentAttendanceDetail.AttendanceTypeCode <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@attendanceTypeCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@attendanceTypeCode", myStudentAttendanceDetail.AttendanceTypeCode);
                    }
                    if (string.IsNullOrEmpty(myStudentAttendanceDetail.Remark))
                    {
                        myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@remark", myStudentAttendanceDetail.Remark);
                    }


                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a StudentAttendanceDetail from the database.
        /// </summary>
        /// <param name="attKey">The AttKey of the StudentAttendanceDetail to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(long attKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAttendanceDetailDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@attKey", attKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the StudentAttendanceDetail class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static StudentAttendanceDetail FillDataRecord(IDataRecord myDataRecord)
        {
            StudentAttendanceDetail myStudentAttendanceDetail = new StudentAttendanceDetail();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AttDetailKey")))
            {
                myStudentAttendanceDetail.AttDetailKey = myDataRecord.GetInt64(myDataRecord.GetOrdinal("AttDetailKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AttKey")))
            {
                myStudentAttendanceDetail.AttKey = myDataRecord.GetInt64(myDataRecord.GetOrdinal("AttKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StudentProfileKey")))
            {
                myStudentAttendanceDetail.StudentProfileKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("StudentProfileKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AttendanceTypeCode")))
            {
                myStudentAttendanceDetail.AttendanceTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("AttendanceTypeCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
            {
                myStudentAttendanceDetail.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myStudentAttendanceDetail.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            return myStudentAttendanceDetail;
        }
    }

}
